package com.atguigu.common.exception;

import com.atguigu.common.result.R;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @program: srb
 * @author: 龙龙
 * @create: 2022-01-22 09:03
 * @description:
 **/
@Component
@RestControllerAdvice
//在controller层添加通知。如果使用@ControllerAdvice，则方法上需要添加@ResponseBody
public class UnifiedExceptionHandler {
/*
    @ExceptionHandler(value = Exception.class)
    public R handleException(Exception e){
        System.out.println(e.getMessage());
        return R.error().message("通用Exception" + e.getMessage() );
    }*/
    /**
    * @Author: 龙龙
    * @Date:  2022/1/22 9:38
    * @Description: jdbc框架特定的异常
    */
    @ExceptionHandler(value = BadSqlGrammarException.class)
    public R handleSqlException(BadSqlGrammarException e){
        System.out.println("SQL异常" + e.getMessage());
        return R.error().message("SQL异常" + e.getMessage());
    }

    /**
     * 自定义异常
     */
    @ExceptionHandler(BusinessException.class)
    public R handleBusinessException(BusinessException e){
//        log.error(e.getMessage(), e);
        return R.error().message(e.getMessage()).code(e.getCode());
    }


}
